package message;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created with Intel IDEA.
 *  点对点 申请应答方
 * @Auther: gjt
 * @Date: 2024/11/3 16:03
 */
public class QLender implements MessageListener {
    private static String USERNAME = ActiveMQConnection.DEFAULT_USER;
    private static String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
    private static String BROKER = ActiveMQConnection.DEFAULT_BROKER_URL;

    private QueueConnection qConnect = null;
    private QueueSession qSession = null;
    private Queue queue = null;

    public QLender(String queuecf, String requestQueue) throws Exception{
        QueueConnectionFactory conFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKER);
        qConnect = conFactory.createQueueConnection();

        // 创建会话
        qSession = qConnect.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

        // 创建队列
        queue = qSession.createQueue(requestQueue);

        qConnect.start();

        // 创建消息监听器
        QueueReceiver receiver = qSession.createReceiver(queue);
        receiver.setMessageListener(this);

        System.out.println("等待 loan 请求...");
    }

    @Override
    public void onMessage(Message message) {
        try {
            boolean accepted = false;

            // 从消息中获取数据
            MapMessage msg = (MapMessage) message;
            double salary = msg.getDouble("salary");
            double loanAmt = msg.getDouble("loanAmt");

            System.out.println("获取的资金" + salary);
            System.out.println("获取的单款" + loanAmt);
            // 决定是否通过审核
            if(loanAmt < 20000){
                accepted = (salary / loanAmt) > .25;
            }else {
                accepted = salary > .33;
            }

            System.out.println("" + "% = " + (salary / loanAmt) + ",loan is" + (accepted ? "Accepted!" : "Declined"));

            // 将结果返回
            TextMessage textMessage = qSession.createTextMessage();
            textMessage.setText(accepted ? "Accepted!" : "Declined");
            textMessage.setJMSCorrelationID(message.getJMSMessageID());

            // 创建发送者并发送消息
            QueueSender sender = qSession.createSender((Queue) message.getJMSReplyTo());
            sender.send(textMessage);
            System.out.println("\n Waiting for loan requests ...");
        } catch (JMSException e) {
            throw new RuntimeException(e);
        }
    }

    private void exit() throws JMSException{
        qConnect.close();
    }

    public static void main(String[] args) throws Exception {
        QLender lender = new QLender("QCF", "QBorrower");

        try{
            BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

            System.out.println("QLender application started");
            System.out.println("Press enter to quit application");
            stdin.readLine();

            lender.exit();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
